package icu.d4peng.basic.satoken;

import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Template;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
import icu.d4peng.basic.satoken.properties.SaOauth2Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.util.Assert;

/**
 * SaOauth2AutoConfiguration:Oauth2自动配置
 *
 * @author d4peng <a href="d4peng@qq.com">d4peng@qq.com</a>
 * @since 2023.12.02
 */
@AutoConfiguration
@EnableConfigurationProperties({ SaOauth2Properties.class })
@ConditionalOnClass(SaOAuth2Manager.class)
@ConditionalOnProperty(prefix = SaOauth2Properties.PREFIX, name = "enabled", havingValue = "true",
		matchIfMissing = true)
public class SaOauth2AutoConfiguration {

	public SaOauth2AutoConfiguration(@Autowired(required = false) SaOAuth2Config saOauth2Config,
			@Autowired(required = false) SaOAuth2Template saOauth2Template) {
		Assert.notNull(saOauth2Config, "saOauth2Config can't be null!");
		SaOAuth2Manager.setConfig(saOauth2Config);
		if (saOauth2Template != null) {
			SaOAuth2Util.saOAuth2Template = saOauth2Template;
		}

	}

}
